<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/9/26
     * Time: 下午5:06
     */

    namespace app\store\logic;

    use app\common\utils\UtilEncryption;
    use app\store\model\AddressModel;
    use app\store\model\CartModel;
    use app\store\model\GoodsModel;
    use app\store\model\OrderInfoModel;
    use app\store\model\OrderModel;
    use think\Db;

    /**
     * 订单管理
     * Class OrderLogic
     *
     * @package app\store\logic
     */
    class OrderLogic
    {

        /**
         * 单个商品下单
         *
         * @param $data
         *
         * @return bool|string
         */
        static public function buyone($data)
        {

            try
            {
                Db::startTrans();


                $Detail = GoodsModel::getDetail($data['goods_id']);
                if (!$Detail)
                {
                    return ['code' => 500,'msg' => '商品部存在！','data' => []];
                }
                $order['order_sn'] = UtilEncryption::MakeSn();
                $order['pay_sn']   = UtilEncryption::MakeSn();

                $order['buyer_id'] = $data['buyer_id'];
                $order['address']  = AddressModel::getCompleteAddress($data['address_id']);

                $order['remark'] = $data['remark'];

                $order_info['goods_id']      = $Detail['id'];
                $order_info['goods_image']   = $Detail['image'];
                $order_info['goods_name']    = $Detail['name'];
                $order_info['goods_price']   = $Detail['price'];
                $order_info['goods_company'] = $Detail['company'];
                $order_info['goods_num']     = $data['goods_number'];
                $order_info['order_sn']      = $order['order_sn'];
                $order_info['buyer_id']      = $data['buyer_id'];

                $order_info['point']        = $Detail['point'] * $data['goods_number'];
                $order_info['goods_amount'] = $Detail['price'] * $data['goods_number'];

                $order['amount'] = $order_info['goods_amount'];
                $order['point']  = $order_info['point'];
                OrderInfoModel::create($order_info);

                OrderModel::create($order);

                Db::commit();

                return ['code' => 200,'msg' => '','data' => ['order_sn' => $order['order_sn']]];
            }
            catch (\Exception $exception)
            {
                Db::rollback();
                return ['code' => 500,'msg' => '下单失败','data' => $exception];
            }

        }

        /**
         * 购物车商品下单
         *
         * @param $data
         *
         * @return bool|string
         */
        static public function buycart($data)
        {

            try
            {
                Db::startTrans();

                $cart_list = CartModel::whereIn('goods_id',$data['cart_ids'])->select();
                if (!$cart_list)
                {
                    return ['code' => 500,'msg' => '购物车找不到你要的商品了！','data' => []];
                }

                $order['order_sn'] = UtilEncryption::MakeSn();
                $order['pay_sn']   = UtilEncryption::MakeSn();

                $order['buyer_id'] = $data['buyer_id'];
                $order['address']  = AddressModel::getCompleteAddress($data['address_id']);

                $order['remark'] = $data['remark'];

                $order['amount'] = 0;
                $order['point']  = 0;
                foreach ($cart_list as $item)
                {
                    $Detail = GoodsModel::getDetail($item['goods_id']);

                    $order_info['goods_id']      = $Detail['id'];
                    $order_info['goods_image']   = $item['goods_image'];
                    $order_info['goods_name']    = $Detail['name'];
                    $order_info['goods_price']   = $Detail['price'];
                    $order_info['goods_company'] = $Detail['company'];
                    $order_info['goods_num']     = $item['goods_num'];
                    $order_info['order_sn']      = $order['order_sn'];
                    $order_info['buyer_id']      = $data['buyer_id'];


                    $order_info['point']        = $Detail['point'] * $item['goods_num'];
                    $order_info['goods_amount'] = $Detail['price'] * $item['goods_num'];

                    $order['amount']           = $order['amount'] + $order_info['goods_amount'];
                    $order['point']            = $order['point'] + $order_info['point'];
                    $order_info['create_time'] = time();
                    $order_info['update_time'] = time();
                    OrderInfoModel::create($order_info);
                }
                $order['create_time'] = time();
                $order['update_time'] = time();
                OrderModel::create($order);

                Db::commit();

                return ['code' => 200,'msg' => '','data' => ['order_sn' => $order['order_sn']]];
            }
            catch (\Exception $exception)
            {
                Db::rollback();
                return ['code' => 500,'msg' => '下单失败','data' => $exception];
            }
        }

        static public function buyer($order_sn,$pay_type)
        {

            $order = OrderModel::where(['order_sn' => $order_sn])->find();

            $order['pay_sn'] = UtilEncryption::MakeSn();


            $order_info['pay_sn'] = $order['pay_sn'];

            if ($pay_type == 4)
            {
                $data['goods_amount'] = 0;
            }
            else
            {
                $data['point'] = 0;
            }

            if ($pay_type == 3)
            {
                return self::wallet($data['order_sn'],$order,$order_info);
            }
        }

        static private function wallet($order_sn,$order,$order_info)
        {

            return self::save($order_sn,$order,$order_info);
        }

        static private function save($order_sn,$order,$order_info)
        {

            try
            {
                Db::startTrans();


                OrderInfoModel::update($order_info,['order_sn' => $order_sn]);

                OrderModel::update($order,['order_sn' => $order_sn]);

                Db::commit();
                return true;
            }
            catch (\Exception $exception)
            {
                Db::rollback();
                return false;
            }
        }
    }